/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2018年1月2日 下午4:42:56
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */  
package com.itttq.gshop.pbpm.provider;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.jdbc.SQL;

import com.itttq.gshop.domain.po.UserScorePo;
import com.itttq.gshop.pbpm.cmpt.mybatis.CustomerSQL;

/**   
 * @ClassName:  UserScorePoSqlProvider
 * @author: 迷蝶
 * @date:   2018年1月2日 下午4:42:56 
 * @Description: TODO
 *     
 * @Copyright: 2018 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
public class UserScoreSqlProvider {
	public static final String TABLE_NAME = "user_score";

	/**
	 * 查找的SQL语句组装<br>
	 * 约定：分页两个参数为：start - 起始位置； size- 每页数量
	 * 
	 * @param userScore
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserScores(final UserScorePo userScore) {
		return new CustomerSQL() {{
			SELECT("`change`,`change_reason_code`,`change_reason_desc`,`create_time`,`id`,`score_after`,`score_befor`,`score_change`,`user_id`,`user_name` ");
			FROM(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userScore.getChange())) {
				WHERE("`change`=#{change}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonCode())) {
				WHERE("change_reason_code=#{changeReasonCode}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonDesc())) {
				WHERE("change_reason_desc=#{changeReasonDesc}");
			}
			if (null != userScore.getScoreAfter()) {
				WHERE("score_after=#{scoreAfter}");
			}
			if (null != userScore.getScoreBefor()) {
				WHERE("score_befor=#{scoreBefor}");
			}
			if (null != userScore.getScoreChange()) {
				WHERE("score_change=#{scoreChange}");
			}
			if (null != userScore.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userScore.getUserName())) {
				WHERE("user_name=#{userName}");
			}
			if(StringUtils.isNotBlank(userScore.getSortField())) {
				ORDER_BY(userScore.getSortField() + (StringUtils.isNotBlank(userScore.getSortMethod()) ? "desc" : userScore.getSortField()));
			} else {
				ORDER_BY("id desc");
			}
			if(null != userScore.getStart() && null != userScore.getSize()) {
				LIMIT(userScore.getStart(), userScore.getSize());
			}
		}}.toString();
	}
	
	/**
	 * 查找记录总数的SQL语句组装
	 * 
	 * @param userScore
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserScoreCount(final UserScorePo userScore) {
		return new SQL() {{
			SELECT("count(*)");
			FROM(TABLE_NAME);
			if (StringUtils.isNotBlank(userScore.getChange())) {
				WHERE("`change`=#{change}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonCode())) {
				WHERE("change_reason_code=#{changeReasonCode}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonDesc())) {
				WHERE("change_reason_desc=#{changeReasonDesc}");
			}
			if (null != userScore.getScoreAfter()) {
				WHERE("score_after=#{scoreAfter}");
			}
			if (null != userScore.getScoreBefor()) {
				WHERE("score_befor=#{scoreBefor}");
			}
			if (null != userScore.getScoreChange()) {
				WHERE("score_change=#{scoreChange}");
			}
			if (null != userScore.getUserId()) {
				WHERE("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userScore.getUserName())) {
				WHERE("user_name=#{userName}");
			}
		}}.toString();
	}
	
	/**
	 * 根据ID查找的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String findUserScoreById(final Integer id) {
		return new SQL() {{
			SELECT("*");
			FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 增加的SQL语句组装
	 * 
	 * @param userScore
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String addUserScore(final UserScorePo userScore) {
		return new SQL() {{
			INSERT_INTO(TABLE_NAME);
			if (StringUtils.isNotBlank(userScore.getChange())) {
				VALUES("`change`","#{change}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonCode())) {
				VALUES("change_reason_code","#{changeReasonCode}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonDesc())) {
				VALUES("change_reason_desc","#{changeReasonDesc}");
			}
			if (null != userScore.getScoreAfter()) {
				VALUES("score_after","#{scoreAfter}");
			}
			if (null != userScore.getScoreBefor()) {
				VALUES("score_befor","#{scoreBefor}");
			}
			if (null != userScore.getScoreChange()) {
				VALUES("score_change","#{scoreChange}");
			}
			if (null != userScore.getUserId()) {
				VALUES("user_id","#{userId}");
			}
			if (StringUtils.isNotBlank(userScore.getUserName())) {
				VALUES("user_name","#{userName}");
			}
		}}.toString();
	}

	/**
	 * 修改的SQL语句组装
	 * 
	 * @param userScore
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String updateUserScore(final UserScorePo userScore) {
		return new SQL() {{
			UPDATE(TABLE_NAME);
			
			if (StringUtils.isNotBlank(userScore.getChange())) {
				SET("`change`=#{change}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonCode())) {
				SET("change_reason_code=#{changeReasonCode}");
			}
			if (StringUtils.isNotBlank(userScore.getChangeReasonDesc())) {
				SET("change_reason_desc=#{changeReasonDesc}");
			}
			if (null != userScore.getScoreAfter()) {
				SET("score_after=#{scoreAfter}");
			}
			if (null != userScore.getScoreBefor()) {
				SET("score_befor=#{scoreBefor}");
			}
			if (null != userScore.getScoreChange()) {
				SET("score_change=#{scoreChange}");
			}
			if (null != userScore.getUserId()) {
				SET("user_id=#{userId}");
			}
			if (StringUtils.isNotBlank(userScore.getUserName())) {
				SET("user_name=#{userName}");
			}
			WHERE("id = #{id}");
		}}.toString();
	}
	
	/**
	 * 根据ID删除的SQL语句组装
	 * 
	 * @param id
	 * @return
	 * @Author 迷蝶
	 * @return String
	 */
	public String delUserScore(final Integer id) {
		return new SQL() {{
			DELETE_FROM(TABLE_NAME);
			WHERE("id = #{id}");
		}}.toString();
	}
}
